from distutils.core import setup
import glob, sys, py2exe, os, time, shutil

VERSION = '1.5'
DIR = 'SSHClient_WX'

def remove_folder(path):
    for obj in glob.glob(os.path.join(path, "*")):
        try:
            os.remove(obj)
        except:
            try:
                os.rmdir(obj)
            except:
                remove_folder(obj)
    os.rmdir(path)

#create file version
def createVersionFile():
    myfile = open(DIR+'/version.txt', 'w')             # open for output (creates)
    myfile.write(VERSION)        # write a line of text
    myfile.close()

def copyImageFolder():
    sourcePath = 'image'
    destPath = 'SSHClient_WX\image'
    os.makedirs('SSHClient_WX\image')
    for root, dirs, files in os.walk(sourcePath):
        dest = destPath + root.replace(sourcePath, '')

        if not os.path.isdir(dest):
            os.mkdir(dest)
            print 'Directory created at: ' + dest

        for f in files:
            oldLoc = root + '\\' + f
            newLoc = dest + '\\' + f
            if not os.path.isfile(newLoc):
                try:
                    shutil.copy2(oldLoc, newLoc)
                    print 'File ' + f + ' copied.'
                except IOError:
                    print 'file "' + f + '" already exists'
                    
def copyFontsFolder():
    sourcePath = 'fonts'
    destPath = 'SSHClient_WX\fonts'
    os.makedirs('SSHClient_WX\fonts')
    for root, dirs, files in os.walk(sourcePath):
        dest = destPath + root.replace(sourcePath, '')

        if not os.path.isdir(dest):
            os.mkdir(dest)
            print 'Directory created at: ' + dest

        for f in files:
            oldLoc = root + '\\' + f
            newLoc = dest + '\\' + f
            if not os.path.isfile(newLoc):
                try:
                    shutil.copy2(oldLoc, newLoc)
                    print 'File ' + f + ' copied.'
                except IOError:
                    print 'file "' + f + '" already exists'

sys.path.append('..')

sys.argv.extend("py2exe --bundle 3".split())
includes = ["paramiko"]
setup(
    name='SSHClient_WX',
    version=VERSION,
    zipfile=None,
    options = {"py2exe":
        {
            "includes":includes,
            'dist_dir': DIR,
            'excludes': ["paramiko"],
            "dll_excludes": ["mswsock.dll", "MSWSOCK.dll", "powrprof.dll"]
        }
    },

    windows = [{
        "script": "update.py",
        "icon_resources":[(0,"house_32.ico")]},
               {
        "script": "SSHClient_WX.py",
        "icon_resources":[(0,"house_32.ico")]}
    ],
    
   

   
)

createVersionFile()
copyImageFolder()
copyFontsFolder()
    
os.remove("SSHClient_WX/w9xpopen.exe")
remove_folder("build")
shutil.make_archive(DIR, "zip", os.getcwd()+"/"+DIR)
